<?php
namespace jy_djhome;
defined('BY_JYA') or exit('error');
/*
	订单
*/
class order extends \plugin{
	//订单创建时，执行此方法，执行相关操作
	public function create($params,$uid='',$url=''){
		$uid=$uid?$uid:$_SESSION['uid'];
		$order=array(
			'uniacid'=>$_SESSION['uniacid'],
			'mid'=>$_SESSION['uid'],
			'worker_id'=>$params['worker_id'],
			'price1'=>$params['price1'],
			'price'=>$params['price'],
			'chetype'=>$params['chetype'],
			'chefee'=>$params['chefee'],
			'address'=>ijson_encode($params['address']),
			'name'=>$params['address']['username'],
			'mobile'=>$params['address']['mobile'],
			'ordersn'=>get_ordersn('jy_djhome_order','ordersn'),
			'remark'=>$params['remark'],
			'day'=>$params['day'],
			'time'=>$params['time'],
			'time2'=>$params['time2'],
			'serve_time'=>$params['serve_time'],
			'juli'=>$params['juli'],
		);
		$order['createtime']=TIMESTAMP;
		pdo_insert('jy_djhome_order',$order);
		$orderid=pdo_insertid();
		foreach($params['goods'] as $item){
			$new_order_goods=array(
				'uniacid'=>$_SESSION['uniacid'],
				'mid'=>$order['mid'],
				'orderid'=>$orderid,
				'goodsid'=>$item['id'],
				'worker_id'=>$order['worker_id'],
				'price'=>$item['price'],
				'total'=>$item['total'],
				'title'=>$item['title'],
				'times'=>$item['times'],
				'createtime'=>TIMESTAMP,
			);
			$this->pdo_insert('order_goods',$new_order_goods);
		}
		$params=array(
			'mid'=>$order['mid'],
			'message'=>'您呼叫了一次服务！',
		);
		$this->model('notice')->add_msg($params);
		return $orderid;
	}
	public function get_order($order_id,$merchid=0){
		if($this->order && $this->order['id']==$order_id){
			return $this->order;
		}
		$this->order=$this->get('order',array('id'=>$order_id,'uniacid'=>$_SESSION['uniacid']));
		return $this->order;
	}
	public function get_fee($serve_id,$waittime=0,$gocity=''){
		return $fees;
	}
	//订单取消
	public function order_cancel($order_id,$istask=false){
		$this->get_order($order_id);
		if(!$this->order || ($this->order['status'] !=0 && $this->order['status'] !=2 && $this->order['status'] !=6)){
			if($istask){
				return false;
			}else{
				exi('订单不存在或者不支持取消！','error');
			}
		}
		$this->update('order',array('status'=>3),array('id'=>$order_id));
		$this->pdo_update('order_goods',array('status'=>3),array('uniacid'=>$_SESSION['uniacid'],'orderid'=>$order_id));
		$params=array(
			'mid'=>$this->order['mid'],
			'message'=>'您的《'.$this->order['start_seat'].'~~'.$this->order['dest_seat'].'》订单已取消！',
		);
		$this->model('notice')->add_msg($params);
		if($this->order['worker_id']){
			pdo_update('jy_djhome_worker',array('busy'=>1),array('id'=>$this->order['worker_id']));
		}
		return true;
	}
	//支付
	public function order_pay($order_id,$params,$url=''){
		$this->get_order($order_id);
		if(!$this->order || $this->order['ispay']){
			exi('订单不存在或者已支付！','error');
		}
		$update=array(
			'paytype'=>$params['paytype'],
			'transid'=>$params['transid'],
		);
		$update['status']=1;
		if($params['paytype']=='hdfk'){
			$update['ispay']=0;
		}else{
			$update['ispay']=1;
			$update['pay_time']=TIMESTAMP;
		}
		$params=array(
			'mid'=>$this->order['mid'],
			'message'=>'您呼叫了一次服务！',
		);
		$this->model('notice')->add_msg($params);
		$this->pdo_update('order',$update,array('uniacid'=>$_SESSION['uniacid'],'id'=>$order_id));
		$this->pdo_update('order_goods',array('status'=>1),array('uniacid'=>$_SESSION['uniacid'],'orderid'=>$order_id));
		return true;
	}
	public function add_baobei($order_id,$msg='',$oldstatus=2,$mid=0){
		$mid=$mid?:$_SESSION['uid'];
		$data=array(
			'uniacid'=>$_SESSION['uniacid'],
			'oldstatus'=>$oldstatus?:2,
			'order_id'=>$order_id,
			'mid'=>$mid,
			'msg'=>$msg?:'异常报备',
			'status'=>0,
			'createtime'=>TIMESTAMP,
		);
		$this->pdo_insert('baobei_log',$data);
		$logid=pdo_insertid();
		return $logid;
	}
	public function change_fee($order_id){
		$this->get_order($order_id);
		$setting=$this->plugin_setting('setting');
		if($setting['fee_price']){
			$fee=$setting['fee_price'];
		}elseif($setting['fee_rate']){
			$fee=$setting['fee_rate']*$this->order['price']/100;
		}
		$mid=pdo_getcolumn('jy_djhome_worker',array('id'=>$this->order['worker_id']),'mid');
		model('core/member')->cut_credit2($mid,$fee,'技师扣手续费','jy_djhome');
		return true;
	}
}